<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="用户id" prop="userId">
        <el-input v-model="formData.userId" placeholder="请输入用户id" />
      </el-form-item>
      <el-form-item label="openid" prop="openid">
        <el-input v-model="formData.openid" placeholder="请输入openid" />
      </el-form-item>
      <el-form-item label="机构名称" prop="orgName">
        <el-input v-model="formData.orgName" placeholder="请输入机构名称" />
      </el-form-item>
      <el-form-item label="机构编码" prop="orgCode">
        <el-input v-model="formData.orgCode" placeholder="请输入机构编码" />
      </el-form-item>
      <el-form-item label="姓名" prop="patientName">
        <el-input v-model="formData.patientName" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="身份证号" prop="idCardNo">
        <el-input v-model="formData.idCardNo" placeholder="请输入身份证号" />
      </el-form-item>
      <el-form-item label="病人ID" prop="patientId">
        <el-input v-model="formData.patientId" placeholder="请输入病人ID" />
      </el-form-item>
      <el-form-item label="门诊号" prop="clinicNo">
        <el-input v-model="formData.clinicNo" placeholder="请输入门诊号" />
      </el-form-item>
      <el-form-item label="订单号" prop="orderNumber">
        <el-input v-model="formData.orderNumber" placeholder="请输入订单号" />
      </el-form-item>
      <el-form-item label="订单类型" prop="orderType">
        <el-select v-model="formData.orderType" placeholder="请选择订单类型">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="订单名称" prop="orderName">
        <el-input v-model="formData.orderName" placeholder="请输入订单名称" />
      </el-form-item>
      <el-form-item label="金额" prop="amout">
        <el-input v-model="formData.amout" placeholder="请输入金额" />
      </el-form-item>
      <el-form-item label="操作人员" prop="operator">
        <el-input v-model="formData.operator" placeholder="请输入操作人员" />
      </el-form-item>
      <el-form-item label="操作类型" prop="operateType">
        <el-select v-model="formData.operateType" placeholder="请选择操作类型">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="结账id" prop="settleId">
        <el-input v-model="formData.settleId" placeholder="请输入结账id" />
      </el-form-item>
      <el-form-item label="发票类型" prop="invoiceType">
        <el-select v-model="formData.invoiceType" placeholder="请选择发票类型">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="发票号" prop="invoiceNo">
        <el-input v-model="formData.invoiceNo" placeholder="请输入发票号" />
      </el-form-item>
      <el-form-item label="发票编码" prop="invoiceCode">
        <el-input v-model="formData.invoiceCode" placeholder="请输入发票编码" />
      </el-form-item>
      <el-form-item label="发票地址" prop="invoiceUrl">
        <el-input v-model="formData.invoiceUrl" placeholder="请输入发票地址" />
      </el-form-item>
      <el-form-item label="校验码" prop="checkCode">
        <el-input v-model="formData.checkCode" placeholder="请输入校验码" />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="formData.remark" placeholder="请输入备注" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="formData.status">
          <el-radio label="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { InvoiceApi, InvoiceVO } from '@/api/hospital/invoice'

/** 发票 表单 */
defineOptions({ name: 'InvoiceForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  userId: undefined,
  openid: undefined,
  orgName: undefined,
  orgCode: undefined,
  patientName: undefined,
  idCardNo: undefined,
  patientId: undefined,
  clinicNo: undefined,
  orderNumber: undefined,
  orderType: undefined,
  orderName: undefined,
  amout: undefined,
  operator: undefined,
  operateType: undefined,
  settleId: undefined,
  invoiceType: undefined,
  invoiceNo: undefined,
  invoiceCode: undefined,
  invoiceUrl: undefined,
  checkCode: undefined,
  remark: undefined,
  status: undefined
})
const formRules = reactive({
  userId: [{ required: true, message: '用户id不能为空', trigger: 'blur' }],
  openid: [{ required: true, message: 'openid不能为空', trigger: 'blur' }],
  patientName: [{ required: true, message: '姓名不能为空', trigger: 'blur' }],
  patientId: [{ required: true, message: '病人ID不能为空', trigger: 'blur' }],
  orderNumber: [{ required: true, message: '订单号不能为空', trigger: 'blur' }],
  orderType: [{ required: true, message: '订单类型不能为空', trigger: 'change' }],
  orderName: [{ required: true, message: '订单名称不能为空', trigger: 'blur' }],
  amout: [{ required: true, message: '金额不能为空', trigger: 'blur' }],
  operator: [{ required: true, message: '操作人员不能为空', trigger: 'blur' }],
  operateType: [{ required: true, message: '操作类型不能为空', trigger: 'change' }],
  settleId: [{ required: true, message: '结账id不能为空', trigger: 'blur' }],
  status: [{ required: true, message: '状态不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await InvoiceApi.getInvoice(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as InvoiceVO
    if (formType.value === 'create') {
      await InvoiceApi.createInvoice(data)
      message.success(t('common.createSuccess'))
    } else {
      await InvoiceApi.updateInvoice(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    userId: undefined,
    openid: undefined,
    orgName: undefined,
    orgCode: undefined,
    patientName: undefined,
    idCardNo: undefined,
    patientId: undefined,
    clinicNo: undefined,
    orderNumber: undefined,
    orderType: undefined,
    orderName: undefined,
    amout: undefined,
    operator: undefined,
    operateType: undefined,
    settleId: undefined,
    invoiceType: undefined,
    invoiceNo: undefined,
    invoiceCode: undefined,
    invoiceUrl: undefined,
    checkCode: undefined,
    remark: undefined,
    status: undefined
  }
  formRef.value?.resetFields()
}
</script>